#include"epoll_add_del.h"

/*

EPOLLONESHOT修饰的socket仅能被触发一次，如后续还需触发该socket，需重新设置EPOLLONESHOT

*/
//设置文件描述符非阻塞
void setnonblocking(int fd){
    int flag =fcntl(fd,F_GETFL);
    flag |= O_NONBLOCK;
    fcntl(fd,F_SETFL,flag);

}

void addfd(int epollfd,int fd, bool one_shot){
    epoll_event event;
    //EPOLLRDHUP.对端关闭，会触发，即可以明确是退出，而不是触发EPOLLIN有数据来
    event.events = EPOLLIN |EPOLLRDHUP; //这会先支持水平触发模式
    event.data.fd = fd;
    if(one_shot){
        event.events |= EPOLLONESHOT;
    }
    epoll_ctl(epollfd,EPOLL_CTL_ADD,fd,&event);

    //设置文件描述符非阻塞
    setnonblocking(fd);



    
}

void removefd(int epollfd,int fd){
    epoll_ctl(epollfd,EPOLL_CTL_DEL,fd,NULL);
    close(fd);

}


void modfd(int epollfd,int fd,int env){
    epoll_event event;
    event.data.fd = fd;
    event.events = env |EPOLLRDHUP|EPOLLONESHOT;
    epoll_ctl(epollfd,EPOLL_CTL_MOD,fd,&event);
}